package com.yuandian.ops.workbench.controller;

import com.yuandian.api.business_monitor.feign.RemoteBusinessDetailService;
import com.yuandian.api.business_monitor.po.DetailedListParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.utils.PageDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 详单查询
 *
 * @author zhoutian
 * @date 2020/10/13 17:32
 */
@Slf4j
@Controller
@RequestMapping("/business/monitoring/detail")
public class BusinessDetailedListController {

    @Autowired
    private RemoteBusinessDetailService businessDetailedListService;


    /**
     * 详单查询列表查询
     *
     * @param params 参数
     * @return Result	通用结果集
     */
    @ResponseBody
    @PostMapping("/getList")
    public R<PageDataInfo> getDetailedList(@RequestBody DetailedListParams params) {
		R<PageDataInfo> result = null;
		try {
			long sTime = System.currentTimeMillis();
			result = businessDetailedListService.getList(params);
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[详单查询]-耗时: {} MS", System.currentTimeMillis() - sTime);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[详单查询]-报错: {} ", e.getMessage());
		}

        return result;
    }


    /**
     * 详单查询下载
     *
     * @param params 参数
     * @return Result	通用结果集
     */
    @ResponseBody
    @PostMapping("/download")
    public R<Object> export(@RequestBody DetailedListParams params) {
        // 设置默认排序
        if (params.getOrderByColumn() == null || ("").equals(params.getOrderByColumn())) {
            params.setOrderByColumn("endTimeUsec");
            params.setIsAsc("desc");
        }

		try {
			long sTime = System.currentTimeMillis();
			businessDetailedListService.download(params);	// TODO	待测试
			log.info("[运维工作台]-[远程调用-BPM模块服务]-[详单查询下载]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM模块服务]-[详单查询下载]报错: " + e.getMessage());
		}

       return R.ok();
    }
}
